package Vis;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import comms.Communicator;

public class Visualiser extends JFrame{

	private static final long serialVersionUID = 1L;
	
	JPanel devices;
	int numDevices = 0;
	//JLabel connectedDevicesLabel;

	private JScrollPane scroll;

	private MenuItem item; 

	public static final int WINDOW_WIDTH = 1124;
	public static final int WINDOW_HEIGHT = 800;
	
	public void setMenuItemAction(ActionListener action) {
		item.addActionListener(action);
	}
	
	public Visualiser(){
		
		MenuBar menubar = new MenuBar();
		item = new MenuItem("Disconnect devices");
		
		Menu menu = new Menu("Devices");
		menu.add(item);
		menubar.add(menu);
		this.setMenuBar(menubar);
		
		/* device panels */
		devices = new JPanel(new GridLayout(2,0));//new FlowLayout(FlowLayout.LEADING));
		//devices.setBorder(BorderFactory.createTitledBorder("Devices"));		
		
		scroll = new JScrollPane(devices,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		TitledBorder t = BorderFactory.createTitledBorder("Connected devices: " + numDevices);
		t.setTitleJustification(TitledBorder.CENTER);
		scroll.setBorder(t);
		scroll.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT-100));
		
		/* main panel */
		this.getContentPane().setPreferredSize(new Dimension(WINDOW_WIDTH,WINDOW_HEIGHT));
		this.getContentPane().setLayout(new BorderLayout());
		
		/* Main Labels */	
		JPanel topPanel = new JPanel(new BorderLayout());
		/*JLabel title = new JLabel("Kinect Visualisation Tool");
		title.setFont(new Font("Book Antiqua", Font.BOLD, 24));
		title.setHorizontalAlignment(SwingConstants.CENTER);
		topPanel.add(title, BorderLayout.NORTH);*/
		//connectedDevicesLabel = new JLabel();
		//topPanel.add(connectedDevicesLabel);		
		
		//topPanel.add(devices, BorderLayout.CENTER);
		
		this.getContentPane().add(topPanel, BorderLayout.NORTH);
		
		topPanel.add(scroll, BorderLayout.CENTER);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				Communicator.exit(0);
			}
		});		
		
		//position the window
		Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(screensize.width - WINDOW_WIDTH, 0);
		
		this.pack();
		this.validate();  
		
		//this.setBackground(Color.DARK_GRAY);
		this.setTitle("Kinect Connections");
		this.setIconImage(Toolkit.getDefaultToolkit() 
			    .getImage("logo.png"));
		
	}
	
	public void addDevice(DevicePanel p){
		numDevices++;
		devices.add(p);
		
		updateLabels();
	}
	
	public void addMap(JPanel s){
		s.setBackground(Color.getColor("#D7E39A"));
		devices.add(s,1);
		updateLabels();
	}
	
	public void removeDevice(DevicePanel p){
		devices.remove(p);
		numDevices--;
		
		updateLabels();
	}
	
	public void updateLabels(){
		scroll.setBorder(BorderFactory.createTitledBorder("Connected devices: " + numDevices));

		this.validate();
	}
	
}


/***
	
	DISPLAY SENSOR DATA IN A VIS TOOL TO SEE WHAT DATA IS LIKE

**/